import {add, deleteById, getById, getListForPage, update, updateOdsi, updateOdTrailer, updateOdDeclare} from '../../services/logistics/logisticsOrder';
import {message} from 'antd';
import Constants from '../../utils/Constants'

const R = require('ramda');
const dateFormat = "YYYY-MM-DD";

export default {
    namespace: 'logisticsOrder',
    state: {
        page: {},
        list: [],
        dataSource: {}
    },
    reducers: {
        /**
         * 刷新state
         *
         * @param state
         */
        refreshState(state, {payload}) {
            return {...state, ...payload};
        }
    },
    effects: {
        /**
         * 分页获取列表
         *
         * @param call
         * @param put
         *
         */* getListForPage({payload}, {call, put}) {

            if (payload.timeRange != null && payload.timeRange != undefined) {
                if (payload.timeRange.length >= 2) {
                    if (payload.timeRange[0] != '') {
                        let beginTime = payload.timeRange[0].format(dateFormat);
                        payload.beginTime = beginTime;
                    }
                    if (payload.timeRange[1] != '') {
                        let endTime = payload.timeRange[1].format(dateFormat);
                        payload.endTime = endTime;
                    }

                }
            }

            const res = yield call(getListForPage, {...R.omit(['onSuccess', 'onFail'])(payload)});
            if (res && res.data.resultCode === 'ok') {
                yield put({
                    type: 'refreshState',
                    payload: {
                        list: res.data.content.list,
                        page: res.data.content.page,
                    },
                });
            } else {
                if (payload.onFail) payload.onFail();
            }
        },
        /**
         * 根据id获取
         *
         * @param id
         */* getById({payload}, {call, put}) {
            if (payload.id) {
                const res = yield call(getById, {...R.omit(['onSuccess', 'onFail'])(payload)});
                if (res && res.data.resultCode === "ok") {
                    yield put({
                        type: 'refreshState',
                        payload: {
                            dataSource: res.data.content
                        }
                    });
                } else {
                    if (payload.onFail) payload.onFail();
                }
            } else {
                yield put({
                    type: 'refreshState',
                    payload: {
                        dataSource: {}
                    }
                });
            }
        },
        /**
         * 新增
         *
         * @param call
         */* add({payload}, {call}) {
            const res = yield call(add, {...R.omit(['onSuccess', 'onFail'])(payload)});
            if (res && res.data.resultCode === "ok") {
                message.info("物流订单新增成功！");
                if (payload.onSuccess) payload.onSuccess();
            } else {
                message.error("物流订单新增失败！");
                if (payload.onFail) payload.onFail();
            }
        },
        /**
         * 更新订单基本信息
         *
         * @param call
         * @param put
         * @param select
         */
        *update({payload}, {call, put}) {
            const res = yield call(update, {...R.omit(['onSuccess', 'onFail'])(payload)});
            if (res && res.data.resultCode === "ok") {
                message.info("基本信息更新成功！");
                if (payload.onSuccess) payload.onSuccess();
            } else {
                message.error("基本信息更新失败！");
                if (payload.onFail) payload.onFail();
            }
        },

        /**
         * 更新补料
         * @param payload
         * @param call
         * @param put
         * @returns {IterableIterator<*>}
         */
        *updateOdsi({payload}, {call, put}) {
            const res = yield call(updateOdsi, {...R.omit(['onSuccess', 'onFail'])(payload)});
            if (res && res.data.resultCode === "ok") {
                message.info("补料信息更新成功！");
                if (payload.onSuccess) payload.onSuccess();
            } else {
                message.error("补料信息更新失败！");
                if (payload.onFail) payload.onFail();
            }
        },

        /**
         * 更新拖车
         * @param payload
         * @param call
         * @param put
         * @returns {IterableIterator<*>}
         */
            *updateOdTrailer({payload}, {call, put}) {
            const res = yield call(updateOdTrailer, {...R.omit(['onSuccess', 'onFail'])(payload)});
            if (res && res.data.resultCode === "ok") {
                message.info("拖车信息更新成功！");
                if (payload.onSuccess) payload.onSuccess();
            } else {
                message.error("拖车信息更新失败！");
                if (payload.onFail) payload.onFail();
            }
        },

        /**
         * 更新报关
         * @param payload
         * @param call
         * @param put
         * @returns {IterableIterator<*>}
         */
            *updateOdDeclare({payload}, {call, put}) {
            const res = yield call(updateOdDeclare, {...R.omit(['onSuccess', 'onFail'])(payload)});
            if (res && res.data.resultCode === "ok") {
                message.info("报关信息更新成功！");
                if (payload.onSuccess) payload.onSuccess();
            } else {
                message.error("报关信息更新失败！");
                if (payload.onFail) payload.onFail();
            }
        },

        /**
         * 删除
         *
         * @param id
         */
        *deleteById({payload}, {call}) {
            const res = yield call(deleteById, {...R.omit(['onSuccess', 'onFail'])(payload)});
            if (res && res.data.resultCode === "ok") {
                message.info("物流订单删除成功！");
                if (payload.onSuccess) payload.onSuccess();
            } else {
                message.error("物流订单删除失败！");
                if (payload.onFail) payload.onFail();
            }
        }
    },
    subscriptions: {
        setup({history, dispatch}) {
            return history.listen(({pathname}) => {
                const menus = JSON.parse(sessionStorage.getItem(Constants.USER_MENU));
                if (menus && !menus.find(item => item.includes(pathname)) && pathname != '/logout') {
                    document.location.href = "/notAuthorized"
                }
            });
        }
    }
};